<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css" />
    <script src="./bootstrap/js/bootstrap.min.js"></script>

    <!-- fontawesome -->
    <link rel="stylesheet" href="./fontawesome/css/all.min.css" />
    <script src="./fontawesome/js/all.min.js"></script>
    <style>
      body {
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div
      class="row container-fluid position-fixed top-0 start-0 bottom-0 end-0"
    >
      <div class="col-2">
        <div class="list-group list-group-flush">
          <a
            href="./users.html"
            target="_main"
            data-id="navUsers"
            data-action="navLink"
            class="active list-group-item list-group-item-action"
            >用户管理</a
          >
          <a
            href="./departments.html"
            target="_main"
            data-id="navDepartments"
            data-action="navLink"
            class="list-group-item list-group-item-action"
            >部门管理</a
          >
        </div>
      </div>
      <div class="col-10 d-flex flex-column">
        <div class="">
          <nav class="navbar bg-body-tertiary">
            <a href="">Home</a>
            <a href="">Shop</a>
            <a href="">Shop</a>
            <a href="">Shop</a>
          </nav>
        </div>
        <div class="flex-fill d-flex">
          <iframe
            id="mainIframe"
            name="_main"
            class="flex-fill"
            src="./users.html"
            frameborder="0"
          ></iframe>
        </div>
      </div>
    </div>
    <script type="module">
      import { _on } from "./utils/functions.js";

      const navLinks = [
        ...document.querySelectorAll('[data-action="navLink"]'),
      ];
      _on("click", {
        navLink() {
          // 记录导航
          localStorage.currentNav = this.dataset.id;
          localStorage.currentNavUrl = this.href;
          // 激活样式
          navLinks.forEach((el) => el.classList.remove("active"));
          this.classList.add("active");
        },
      });

      // 页面初始化
      window.onload = function () {
        //定义当前导航激活项和ifream的src
        if (localStorage.currentNav !== undefined) {
          navLinks.forEach((el) => el.classList.remove("active"));
          document
            .querySelector(`[data-id="${localStorage.currentNav}"]`)
            ?.classList.add("active");

          // 初始化iframe
          mainIframe.src = localStorage.currentNavUrl;
        }
      };
    </script>
  </body>
</html>
